<?php

include_once APPLICATION_DIR . "/application/model/BOOKING.php";

class Booking  extends DataObjects_BOOKING
{
	public function validateInput(){
        global $error_msg;
		return true;
		return $error_msg->isNoError();
    }

	public function update() {
		return parent::update();
	}

	public function delete() {
		return parent::delete();
	}
    public function status2Text($status){
        return status2Text($status);
    }
    public static function getHour($index){
		if ($index > 47 || $index < 0)
			return false;

		if ($index/2 > floor($index/2)){
			return floor($index/2).':30';
		}
		return ($index/2).':00';
	}
	public static function checkAvailableBooking($date,$listService,$idBookingDB = 0,$bookingSerialize=""){
		if($listService[strlen($listService) - 1] == ','){
			$listService = substr($listService, 0 , strlen($listService) - 1);
		}
		if($listService == "" || !check_date($date)){
			return false;
		}
		$service = new Service_Booking();
		$sql = "SELECT SUM(TIME) TOTAL_TIME FROM SERVICE_BOOKING WHERE ID IN (".$listService.")";
		$service->query($sql);
		$service->fetch();
		$total_time = $service->TOTAL_TIME;
		$date = date_to_date_sql($date);
        
		$indexHours = Booking::getWorkingHours($date);
		$workingHours = array();
		for($ind = $indexHours["START"];$ind<=$indexHours["END"];$ind++){
			$hours = Booking::getHour($ind). ":00";
			if($date != date("Y-m-d") || $hours > date("H:i:s")){
				$workingHours[$hours] = true;
			}
		}
		
		$bookedArr = array();
		if($bookingSerialize!="" && is_array(unserialize($bookingSerialize))){
			$tmp = unserialize($bookingSerialize);
			foreach($tmp as $item){
				$startDate = $item['DATE']. " " .$item['TIME'];
				$total_time = 0;
				if(count($item['SERVICES']) > 0){
					$service = new Service_Booking();
					$sql = "SELECT SUM(TIME) TOTAL_TIME FROM SERVICE_BOOKING WHERE ID IN (".  implode(",", $item['SERVICES']).")";
					$service->query($sql);
					$service->fetch();
					$total_time = $service->TOTAL_TIME;
				}
				$endDate = date("Y-m-d H:i:s", strtotime($startDate) + ($total_time*60));
				$bookedArr[] = array("S_DATE"=>$startDate,"E_DATE"=>$endDate);
			}
		}
		
		$booking = new Booking();
		$sql = "SELECT BDS.ID_BOOKING_DETAIL, BD.ID_TECHNICIAN, BD.BOOKING_DATE S_DATE, (
					BD.BOOKING_DATE + INTERVAL SUM( SB.`TIME` )
					MINUTE
					)E_DATE
					FROM BOOKING B
					INNER JOIN BOOKING_DETAIL BD ON BD.ID_BOOKING = B.ID
					INNER JOIN BOOKING_DETAIL_SERVICE BDS ON BDS.ID_BOOKING_DETAIL = BD.ID
					INNER JOIN SERVICE_BOOKING SB ON SB.ID = BDS.ID_SERVICE_BOOKING
					WHERE DATE( BD.BOOKING_DATE ) = '{$date}' AND B.STATUS <> 2 ".
					($idBookingDB > 0?" AND BD.ID <> ". $idBookingDB. " ":" ") .
					"GROUP BY BDS.ID_BOOKING_DETAIL";
		$booking->query($sql);
		$booked = array();
		while($booking->fetch()){
			$booked[$booking->ID_BOOKING_DETAIL] = array("S_DATE"=>$booking->S_DATE,"E_DATE"=>$booking->E_DATE);
		}
		foreach($workingHours as $hours=>$isEnable){
			$count = 0;
			$sDate = $date . " " . $hours;
			$eDate = date("Y-m-d H:i:s", strtotime($sDate) + ($total_time*60));
			
			foreach($booked as $idBook => $detail){
				if($sDate > $detail['E_DATE'] || $eDate < $detail['S_DATE']){
					continue;
				}
				else{
					$count++;
				}
			}
			if(count($bookedArr)>0){
				foreach($bookedArr as $item){
					if($sDate > $item['E_DATE'] || $eDate < $item['S_DATE']){
						continue;
					}
					else{
						$count++;
					}
				}
			}
			if($count >= NUMBER_TECHNICIAN){
				$workingHours[$hours] = false;
			}
		}
		
		$text = "";
		$count = 0;
		foreach($workingHours as $hour => $isEnable){
			$count++;
			if($isEnable){
				$disable = "";
			}
			else{
				$disable = 'disabled="disabled"';
			}
			$arrHour = explode(":", $hour);
			$text .= '<input type="radio" name="TIME" id="TIME'.$count.'" value="'.$hour.'" '.$disable.' /><label id="ApproveButtonLabel" for="TIME'.$count.'">'.$arrHour[0].":".$arrHour[1].'</label> ';
			if($count%4 == 0){
				$text .= "<br />";
			}
		}
		echo ''.$text.'';
	}
	public static function getWorkingHours($date){
		$day = date("w",strtotime(date_to_date_sql($date)));
		$start = 20;
		$end = 38;
		if($day == 0){
			 $start = 22;
			 $end = 34;
		}
		elseif($day == 6){
			 $start = 20;
			 $end = 36;
		}
		return array("START"=>$start,"END"=>$end);
	}
}
?>